package com.djdevs.calculatorconverter;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xmlpull.v1.XmlPullParserException;

import java.util.*;

import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.net.*;

import java.io.*;

public class ReadXml {
	
	//List to hold Employees object
    private List<Converter> lstConverter = null;
    private Converter conv = null;
    private ConvUnit cnvUnit = null;
	
	public ReadXml(XmlResourceParser xrpConverter) {
		ParseXml(xrpConverter);
	}
	
	public List<Converter> getConverterList() {
        return lstConverter;
    }
	
	public ArrayList<String> GetConverterList()
	{
		ArrayList<String> converterList = new ArrayList<String>();
		for (int i = 0; i < this.lstConverter.size(); i++) {
			Converter conv = this.lstConverter.get(i);
			converterList.add(conv.type);
		}
		
		return converterList;
	}
	
	public ArrayList<String> GetUnitListForConverter(String converterType)
	{
		ArrayList<String> unitList = new ArrayList<String>();
		Converter convForUnit = null;
		
		for (int i = 0; i < this.lstConverter.size(); i++) {
			if (this.lstConverter.get(i).type.equals(converterType)) {
				convForUnit = this.lstConverter.get(i);
				break;
			}
		}
		
		if (convForUnit != null) {
			for (int i = 0; i < convForUnit.lstUnits.size(); i++) {
				unitList.add(convForUnit.lstUnits.get(i).unitName + " ( " + convForUnit.lstUnits.get(i).unitCode + " )"); 
			}
		}
		
		return unitList;
	}
	
	private void ParseXml(XmlResourceParser xrpConverter)
	{
		int eventType = -1;
		try
		{
			while(eventType != XmlResourceParser.END_DOCUMENT)
	    	{
	            if (eventType == XmlResourceParser.START_TAG) {
	                String tagName = xrpConverter.getName();
	                if (tagName.equals("Converters")) {
	                	lstConverter = new ArrayList<Converter>();
	                }
	                else if (tagName.equals("Converter")) {
	                	conv = new Converter();
	                	conv.type = xrpConverter.getAttributeValue(1);
	                	conv.baseUnitCode = xrpConverter.getAttributeValue(2);
	                	conv.lstUnits = new ArrayList<ConvUnit>();
					}
	                else if (tagName.equals("Unit")) {
	                	cnvUnit = new ConvUnit();
	                	cnvUnit.id = Integer.parseInt(xrpConverter.getAttributeValue(0));
	                	cnvUnit.unitCode = xrpConverter.getAttributeValue(1);
	                	cnvUnit.unitName = xrpConverter.getAttributeValue(2);
	                	cnvUnit.conversionFactor = Double.parseDouble(xrpConverter.getAttributeValue(3));
	                	conv.lstUnits.add(cnvUnit);
					}
	            }
	            else if (eventType == XmlResourceParser.END_TAG) {
	            	String tagName = xrpConverter.getName();
	                if (tagName.equals("Converter")) {
	                	lstConverter.add(conv);
					}
				}
	            
	            xrpConverter.next();
	            eventType = xrpConverter.getEventType();
	    	}
		}
	    catch (XmlPullParserException e) {
	        e.printStackTrace();
	    }
		catch (IOException e) {
			e.printStackTrace();
		} 
		catch (Exception e) {
			e.printStackTrace();
		} 
	}
}